package com.chujian.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.ForEach;

import com.chujian.dao.FoodListDao;
import com.chujian.dao.OrderDao;
import com.chujian.daoImpl.FoodListDaoImpl;
import com.chujian.daoImpl.OrderDaoImpl;
import com.chujian.model.FoodList;
import com.chujian.model.Order;
import com.google.gson.Gson;

/**
 * Servlet implementation class OrderCurrentListServletConsumer
 */
@WebServlet("/OrderCurrentListServletConsumer")
public class OrderCurrentListServletConsumer extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private List<Order> mListOrder;
    private List<FoodList>mList;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderCurrentListServletConsumer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String userPhone=request.getParameter("userPhone");
		String order_status=request.getParameter("orderStatus");
		request.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		System.out.println(userPhone+":"+order_status);
		int orderStatus=-1;
		try {
			orderStatus = Integer.parseInt(order_status);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		String str=new String(phoneNumb.getBytes("iso-8859-1"),"UTF-8");
		OrderDao orderDao=new OrderDaoImpl();
		FoodListDao foodListDao=new FoodListDaoImpl();
		if(userPhone!=null&&order_status!=null){
			mListOrder=orderDao.qeuryByPhoneAndStatus(userPhone, orderStatus);
			for (int i=0;i<mListOrder.size();i++) {
				List<FoodList> listFood=foodListDao.qeuryByOrderId(mListOrder.get(i).getOrderId());
				mListOrder.get(i).setFoodList(listFood);
			}
				
			
		}
		Gson gson=new Gson();
		String jsonString =gson.toJson(mListOrder);
		System.out.println(mListOrder);
		out.write(jsonString);
	}

}
